<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userRegisterService, userLoginService } from '@/api/user'
//控制注册与登录表单的显示， 默认显示注册
const isRegister = ref(true)
const registerData = ref({
    password: '',
    repassword: '',
    oldpassword: ''
})

const checkRePassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次确认密码'))
    } else if (value !== registerData.value.password) {
        callback(new Error('密码不一致'))
    } else {
        callback()
    }
}

const rules = {
    
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5到16位非空字符', trigger: 'blur' }

    ],
    repassword: [
        { validator: checkRePassword, trigger: 'blur' }
    ]
}
const router = useRouter();
import {useTokenStore} from '@/stores/token'
import useuserinfostore from '@/stores/userinfo'
import { repasswordService } from '@/api/user'
const userinfostore = useuserinfostore();
const tokenStore = useTokenStore();
const repassword = async() => {
    let result = await repasswordService(registerData);
    userinfostore.removeinfo();
    tokenStore.deleteToken();
    router.push('/login')
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerData"
                    :rules="rules">
                    
                    <el-form-item prop="oldpassword">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入旧密码"
                            v-model="registerData.oldpassword"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
                            v-model="registerData.password"></el-input>
                    </el-form-item>
                    <el-form-item prop="repassword">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请再次输入密码"
                            v-model="registerData.repassword"></el-input>
                    </el-form-item>
                    <!-- 注册按钮 -->
                    <el-form-item>
                        <el-button class="button" type="primary" @click="repassword" auto-insert-space>
                            重置密码
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>

</template>
<style lang="scss" scoped>
/* 样式 */
.login-page {
    height: 100vh;
    background-color: #fff;

    .bg {
        background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
            url('@/assets/login_bg.jpg') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>